var mysql = require('mysql');
var pconfig = require("../paca/pconfig");
/*
con = pmysql.GetCon();
con.query('SELECT * FROM ttt', function(err, rows, fields) {
    if (err) throw err;
    console.log('The solution is: ', rows);
});

conn.end();
*/

var PCon = null;


//MY SQL 链接丢失的时候 ， 重新建立连接
var handleKFDisconnect = function() {
    PCon.on('error', function(err) {
        if (!err.fatal) {
            return;
        }
        if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
            //console.log("PROTOCOL_CONNECTION_LOST");
            throw err;
        }
        //log.error("The database is error:" + err.stack);

        //kfdb = mysql.createConnection(kf_config);
        var con = pconfig.GetCon();
        PCon = mysql.createConnection({
			hot:con.mysqladd,
			user:con.mysqluser,
			password:con.mysqlpasswd,
			database:con.mysqldb,
			port:con.mysqlport
		});
		PCon.connect();
        //console.log("kfid");

        //console.log(kfdb);
        handleKFDisconnect();
    });
};

module.exports.GetCon= function()
{
	var con = pconfig.GetCon();
	if(PCon == null)
	{
		PCon = mysql.createConnection({
			hot:con.mysqladd,
			user:con.mysqluser,
			password:con.mysqlpasswd,
			database:con.mysqldb,
			port:con.mysqlport
		});
		PCon.connect();
		handleKFDisconnect();
	}
	return PCon;
}


